package com.example.szh.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.szh.R;
import com.example.szh.action.BroadAction;
import com.example.szh.adapter.UserAdapter;
import com.example.szh.adapter.UserMenuAdapter;
import com.example.szh.db.DBManager;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.UserEntity;
import com.example.szh.entity.UserItemEntity;
import com.example.szh.entity.UserMenuEntity;
import com.example.szh.mvp.contract.UserContract;
import com.example.szh.mvp.di.DaggerUserConponent;
import com.example.szh.mvp.di.UserModules;
import com.example.szh.mvp.presenter.UserPresenter;
import com.example.szh.mvp.view.user.HomePageActivity;
import com.example.szh.mvp.view.user.LoginActivity;
import com.example.szh.mvp.view.user.QuitActivity;
import com.example.szh.mvp.view.user.UserActivity;
import com.example.szh.nextwork.view.BaseFragment;
import com.example.szh.utils.LoadImage;
import com.example.szh.widget.HomeGrideDecoration;

import java.util.List;


public class UserFragment extends BaseFragment<UserPresenter> implements UserContract.IUserView {

    private View menuView, timeLineView;
    private RecyclerView userRv;
    private RecyclerView menuRv;
    private UserAdapter userAdapter;
    private UserMenuAdapter userMenuAdapter;
    private TextView username;
    private UserBroadCast broadCast;
    private SharedPreferences isLogin;
    private boolean is;
    private ImageView menu_head_img;
    private TextView menu_user;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        broadCast=new UserBroadCast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadAction.UPDATAUSERACTION);
        getActivity().registerReceiver(broadCast, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(broadCast);
    }

    @Override
    public void refresh(BaseEntity baseEntity) {

    }

    @Override
    public void refreshMenu(List<UserMenuEntity> menuEntities) {
        userMenuAdapter.setNewData(menuEntities);
    }

    @Override
    public void refreshItem(List<UserItemEntity> itemEntities) {
        userAdapter.setNewData(itemEntities);
        userAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (itemEntities.get(position).name.equals("夜间模式")){
                    boolean selFlag = itemEntities.get(position).selFlag;
                    if (selFlag){
                        userAdapter.getData().get(position).setSelFlag(false);
                    }else {
                        userAdapter.getData().get(position).setSelFlag(true);
                    }
                }
                userAdapter.notifyDataSetChanged();
                if (itemEntities.get(position).name.equals("更多设置")){
                    Intent intent = new Intent(getActivity(), QuitActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_user;
    }

    @Override
    public void initView() {

        userRv = (RecyclerView) f(R.id.user_rv);
        menuView= LayoutInflater.from(getActivity()).inflate(R.layout.view_user_menu,null);
        timeLineView= LayoutInflater.from(getActivity()).inflate(R.layout.view_user_speed,null);
        menuRv=menuView.findViewById(R.id.menu_rv);
        menu_head_img = menuView.findViewById(R.id.menu_head_img);
        username = menuView.findViewById(R.id.user_name);
        menu_user = menuView.findViewById(R.id.menu_user);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(getContext(),4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        menuRv.setLayoutManager(gridLayoutManager);
        userMenuAdapter=new UserMenuAdapter();
        menuRv.setAdapter(userMenuAdapter);
        LinearLayoutManager manager=new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        userRv.setLayoutManager(manager);
        userAdapter=new UserAdapter();
        userAdapter.addHeaderView(menuView);
        userAdapter.addHeaderView(timeLineView);
        userRv.setAdapter(userAdapter);
        userRv.addItemDecoration(new HomeGrideDecoration(getContext()));
        username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                is = isLogin.getBoolean("is", false);
                if (is){
                    Intent intent1 = new Intent(getActivity(), UserActivity.class);
                    startActivity(intent1);
                }
            }
        });
        menu_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HomePageActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void inject() {
        DaggerUserConponent.builder().userModules(new UserModules(this)).build().inject(this);
    }

    @Override
    public void initData() {
        isLogin = getActivity().getSharedPreferences("isLogin", Context.MODE_PRIVATE);

        p.initData();


    }

    //广播接收器接受传过来的数据
    private void refreshUser(UserEntity userEntity){
        username.setText(userEntity.getUser_name()+"LV"+userEntity.getUser_integral());

    }

    @Override
    public void onStart() {
        super.onStart();
        boolean is = isLogin.getBoolean("is", false);
        if (is){
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
                username.setText(list.get(0).getUser_name()+"LV"+list.get(0).getUser_integral());
                LoadImage.loadImg(list.get(0).getUser_img(),menu_head_img);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            menu_head_img.setImageResource(R.mipmap.ic_launcher_round);
            username.setText("登录/注册");
        }

    }

    @Override
    public void onResume() {
        super.onResume();


    }

    private class UserBroadCast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadAction.UPDATAUSERACTION)){
                UserEntity userEntity = intent.getParcelableExtra("userValues");
                refreshUser(userEntity);
            }
        }
    }
}